package com.epam.productxslt.controller;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public final class RequestContent {
	public static String XML_PATH;
	public static String CATEGORIES_XSL_PATH;
	public static String SUBCATEGORIES_XSL_PATH;
	public static String GOODS_XSL_PATH;
	public static String ADD_GOODS_XSL_PATH;
	public static String SAVE_GOODS_XSL_PATH;
	private HashMap<String, String[]> requestParameters;
	private HashMap<String, Object> requestAttributes;
	private HashMap<String, Object> sessionAttributes;
	private String redirectPath;

	private PrintWriter responseWriter;


	public RequestContent (HttpServletRequest request) {
		requestParameters = (HashMap<String, String[]>) request
				.getParameterMap();

		Enumeration<String> requestAttributeNames = request.getAttributeNames();
		requestAttributes = new HashMap<String, Object>();
		while (requestAttributeNames.hasMoreElements()) {
			String name = requestAttributeNames.nextElement();
			Object value = request.getAttribute(name);
			requestAttributes.put(name, value);
		}

		HttpSession session = request.getSession();
		Enumeration<String> sessionAttributeNames = session.getAttributeNames();
		sessionAttributes = new HashMap<String, Object>();
		while (sessionAttributeNames.hasMoreElements()) {
			String name = sessionAttributeNames.nextElement();
			Object value = session.getAttribute(name);
			sessionAttributes.put(name, value);
		}
	}


	public void addInformation(HttpServletRequest request) {
		Set<String> requestAttributNames = requestAttributes.keySet();
		for (String name : requestAttributNames) {
			Object value = getRequestAttribute(name);
			request.setAttribute(name, value);
		}

		HttpSession session = request.getSession();
		Set<String> sessionAttributNames = sessionAttributes.keySet();
		for (String name : sessionAttributNames) {
			Object value = getSessionAttribute(name);
			session.setAttribute(name, value);
		}
	}

	public void setRequestAttribute(String key, Object value) {
		requestAttributes.put(key, value);
	}

	public void setSessionAttribute(String key, Object value) {
		sessionAttributes.put(key, value);
	}

	public String getRequestParameter(String name) {
		if (requestParameters.get(name) != null) {
			return requestParameters.get(name)[0];
		} else {
			return null;
		}
	}

	public String[] getRequestParameterValues(String name) {
		return requestParameters.get(name);
	}

	public Object getRequestAttribute(String name) {
		return requestAttributes.get(name);
	}

	public Object getSessionAttribute(String name) {
		return sessionAttributes.get(name);
	}

	public void sessionClear() {
		sessionAttributes.clear();
	}

	public PrintWriter getResponseWriter() {
		return responseWriter;
	}
	
	public Map<String, String[]> getRequestParametersMap(){
		return requestParameters;
	}

	public void setResponseWriter(PrintWriter responseWriter) {
		this.responseWriter = responseWriter;
	}


	public String getRedirectPath() {
		return redirectPath;
	}


	public void setRedirectPath(String redirectPath) {
		this.redirectPath = redirectPath;
	}
}
